
 world example
#echo hello
# echo "hello Shell"
# echo 'hello Shell!'
#echo "hello shell"
# a="hello shell"
# b = hello
# echo $a $b
#unset a
#取消变量名
# foo="bar"
# foo2='bar'
# echo "val is $foo and $foo2"
#echo $foo $foo2 
# name="Shell"
# readonly name
# name="hello"
# echo $name
# a=1
# unset a
# echo $a
# a=1
# str1="hello $a"
# str2='hello $a'
# echo $str1 $str2
#拼接字符串
#双引号拼接
# name="str one"
# foo1="hello, "$name" ! "
# foo2="hello, ${name} ! "
# echo $foo1 $foo2
# #单引号拼接
# foo3='hello, '$name' !'
# foo4='hello, ${$name} !'
# echo $foo3 $foo4
# 定义数组
# array=(1 2 3 4 5)
# echo ${array[@]} ${array[0]}
# echo "----------打印文本--------------------"
# echo "----------解释特殊字符----------------"
# echo -e "hello,
# Linux,  "
# echo "----------------换行-----------------"
# echo -n "hello Linux!"



#echo "the file is $0"
#echo "the parameter is $1 $2"
#echo "number of parameter is $#"
#echo "all parameter is $*"
#echo $?
# read 
# echo $REPLY
#一次给多个变量赋值
#
# read -p "请输入姓名：" name
# read -p "请输入性别：" sex

# echo "姓名：${name}"
# echo "性别: ${sex}"

# read -t 10 -sp "请输入密码（10s）：" pwd1
# echo 
# read -t 10 -sp "请再次输入密码（10s）：" pwd2
# printf "\n"

# if [ $pwd1 == $pwd2 ]
# then
#  echo "密码一致，验证通过~"
# else
#  echo "密码错误，验证失败！"
# fi

#declare：声明变量内容
# a=1+1
# declare -i b=1+1
# echo $a
# echo $b

# #普通数组
# declare -a arr1=("李明" 33 "hobbit1")
# echo ${arr1[*]}
# echo ${arr1[1]}

# declare -a arr2=([0]="王强" 55 "hobbit2")
# echo ${arr2[@]}

# #关联数组
# declare -A arr3=(["name"]="王武" [age]=34 [hobbit]="hobbit3")
# echo ${arr3[@]}
# echo ${arr3["age"]}

# num1=100
# num2=100
# if test $num1 -eq $num2
# then
#     echo "两个数相等！"
# else
#     echo "两个数不相等！"
# fi

# num1="helloaa"
# num2="hello"
# if test $num1 = $num2
# then
#     echo '两个字符串相等!'
# else
#     echo '两个字符串不相等!'
# fi

#关系运算
#test 1 -lt 2 && echo "yes"
#test "a" = "a" && echo "yes"

#declare -i a=10
#declare -i b=20
#if [ $a == $b ]; then
#echo "a is equal to b"
#fi
#if [ $a != $b ]; then
#echo "a is not equal to b"
#fi

#declare -i c
#echo "input 1 or 2"
#read a
#if [ $a -eq 1 ];then
#echo "1"
#else
#echo "2"
#fi


#expr
# val=`expr 2 + 2`
# echo "和为：$val"
# a=20
# b=10
# echo `expr $a + $b`
# echo `expr $a - $b`
# echo `expr $a \* $b`
# echo `expr $a / $b`
# echo `expr $a % $b`
# echo  `expr $a == $b`
# echo  `expr $a != $b`
# x="xx"
# y="xy"
# echo  `expr $x == $y`
# echo  `expr $x != $y`

# a="abc"
# b="efg"

# if [ $a = $b ]
# then
#     echo "$a = $b, 为真， a 等于 b"
# else
#     echo "$a != $b, 为假,  a 不等于 b"
# fi

# if [ $a != $b ]
# then
#     echo "$a != $b， 为真，  a 不等于 b"
# else
#     echo "$a != $b, 为假，  a 不等于 b"
# fi

# if [ -z $a ]
# then
#     echo "为真，长度为0"
# fi

# if [ -n $a ]
# then
#     echo "为真，长度为 ${#a}"
# fi

# if [ $a ]
# then
#     echo "为真，字符串不为空"
# fi
# a=10
# b=20
#if语句
a=10
b=20
# if [ $a -eq $b ]
# then
#     echo "$a 等于 $b"
# elif [ $a -gt $b ]
# then
#     echo "$a 大于 $b "
# elif [ $a -lt $b ]
# then
#     echo "$a 小于 $b "
# fi
# if test $[a] -lt $[b]
# then
#     echo "$a 小于 $b"
# fi

# if [ $a -eq $b ]
# then
#     echo "$a -eq $b : 为真，a 等于 b"
# else
#     echo "$a -eq $b: 为假，a 不等于 b"
# fi

# if [ $a -ne $b ]
# then
#     echo "$a -ne $b: 为真，a 不等于 b"
# else
#     echo "$a -ne $b : 为假，a 等于 b"
# fi

# if [ $a -gt $b ]
# then
#     echo "$a -gt $b: 为真，a 大于 b"
# else
#     echo "$a -gt $b: 为假，a 不大于 b"
# fi

# if [ $a -lt $b ]
# then
#     echo "$a -lt $b: 为真，a 小于 b"
# else
#     echo "$a -lt $b: 为假，a 不小于 b"
# fi
# if [ $a -ge $b ]
# then
#    echo "$a -ge $b: 为真，a 大于或等于 b"
# else
#     echo "$a -ge $b: 为假，a 小于 b"
# fi

# if [ $a -le $b ]
# then
#     echo "$a -le $b: 为真，a 小于或等于 b"
# else
#     echo "$a -le $b: 为假，a 大于 b"
# fi

# x=10
# y=20

# if [ $x != $y ]
# then
#     echo "$x != $y : 为真，x不等于y"
# else
#     echo "$x != $y : 为假，x等于y"
# fi

# if [ $x -gt 10 -o $y -gt 10 ]
# then
#     echo "为真， $x 大于 10 或 $y 大于 10"
# else
#     echo "为假， $x 和 $y 都不大于10"
# fi

# if [ $x -gt 5 -a $y -gt 5 ] 
# then
#     echo "为真， $x 和 $y 都大于5"
# else
#     echo "为假， $x 和 $y 都不大于5"
# fi

# x=10
# y=20

# if [[ $a -gt 100 && $b -gt 100 ]]
# then
#     echo "true"
# else
#     echo "false"
# fi

# if [[ $a -lt 10 || $b -lt 10 ]]
# then
#     echo "true"
# else
#     echo "false"
# fi
#declare -i e=10
#declare -i f=10
#if [ $a == $b ];then 
#echo "a is equal to b"
#elif [ $a -gt $b ];then 
#echo "e is greater than f"
#else 
#echo "e is less than f"
#fi

#echo "you like"
#echo "1 is apple"
#echo "2 is orange"
#echo "inpput your choice"
#read a
#case $a in
#1)
#echo "you like apple"
#;;
#2)
#echo "you like orange"
#;;
#*)
#echo "nothing"
#exit 1
#esac

# declare -i s
# s=0
# for (( i=1; i<=100; i++ ))
# do 
# s=s+i
# done 
# echo "the sum is $s"

# declare -a A
# A=(1 2 3 4 5 6 7 8 9 10)
# declare -i c
# for i in 1 3 5 7 9
# do 
# c=c+A[$i];
# done 
# echo "the count is $c"

# for val in 1 2 3 4 5
# do
#     echo "The val is : $val"
# done

# #顺序输出字符
# for str in This is a string
# do
#     echo $str
# done

#示例， 循环输出1到5
# val=1
# while(( $val<=5 ))
# do
#     echo $val
#     let "val++"
# done
# val=0
# until [  $val -gt 10 ]
# do
#     echo $val
#     val=`expr $val + 1`
# done

# echo "请输入1到3的数字"
# echo '你输入的数字为：'
# read num
# case $num in
# 1)  echo "你选择了1" ;;
# 2)  echo "你选择了2" ;;
# 3)  echo "你选择了3" ;;
# *)  echo "你没有选择范围内的数字" ;;
# esac

demoFun()
{
    echo "hello Shell, this is the first function"
    return 0
}

# echo "------开始执行------"
# demoFun
# echo "------执行结束------"

# AddFun()
# {
#     echo "计算两数之和"
#     echo "输入第一个数字-> "
#     read num1
#     echo "输入第二个数字->"
#     read num2
#     return $(($num1+$num2))
# }

# AddFun
# echo "两数之和为-> $?"

# fun()
# {
#     echo "参数1-> $1"
#     echo "参数9-> $9"
#     echo "参数10-> $10"
#     echo "参数10-> ${10}"
#     echo "参数11-> ${11}"
#     echo "输出所有参数 $*"
# }
# fun 0 1 2 3 4 5 6 7 8 9 10

# # 判断变量 a 是否为空
# a=""
# if [[ -z $a ]]; then
# echo "a is empty"
# fi

# # 判断变量 b 是否为非空
# b="hello"
# if [[ -n $b ]]; then
# echo "b is not empty"
# fi

# # 判断变量 c 是否以 h 开头，以 o 结尾
# c="hello"
# if [[ $c == h*o ]]; then
# echo "c matches the pattern h*o"
# fi

# # 判断变量 d 是否是一个 IP 地址
# d="192.168.1.1"
# if [[ $d =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
# echo "d is a valid IP address"
# fi

#test和if混用，不用单中括号
# a=10
# b=20
# if test $[a] -lt $[b]
# then
#     echo "$a 小于 $b"
# fi

# # 判断字符串 str 是否为空
# str=""
# if [  -z $str ]; then
# echo "str is empty"
# fi

# arr=(10 20 30 40)
# echo $[arr[0]]

# echo  "------创建多个文件文件------"
# touch {a,b,c,d}.txt
# touch {1..5}.c
# ls

# echo "------创建代码块------"
# #下方代码执行五件事：1.打印hello Shell 2.打印时间 3.展示目录 4.创建文件 5.输出重定向
# { 
#     echo "hello Shell"
#     date
#     ls
#     touch log.txt
# } > log.txt

# echo "------分割变量------"
# val=Shell
# echo "hello$val"
# echo "$valhello"
# echo "${val}hello"

# #结构替换
# #${val:-string}:当val为空，${val:-string}输出a，val不为空时，则输出val
# val1=""
# echo ${val1:-a1} "val1此时值是：$val1"
# val2="hello ---"
# echo ${val2:-a1} "val2此时值是：$val2"
# echo -e "\n"
# echo -e "\n"
# echo -e "\n"



# #${val:+string}:与-string相反,为空${val:+string}输出val， 不为空输出a2
# val3=""
# echo ${val3:+a2} "val3此时值是：$val3"
# val4="hello +++"
# echo ${val4:+a2} "val4此时值是：$val4"
# echo -e "\n"
# echo -e "\n"
# echo -e "\n"

# #${val:=string}:val为空时，${val:=string}输出为a，同时val的值也会被赋值为a，不为空则不作为
# val5=""
# echo ${val5:=a3} "val5此时值是：$val5"
# val6="hello ==="
# echo ${val6:=a3} "val6此时值是：$val6"
# echo -e "\n"
# echo -e "\n"
# echo -e "\n"

# #${val:?string}:val不为空则${val:=string}输出为val，val为空，则会将string输出到标准错误，并且退出脚本
# val7="hello ???"
# echo ${val7:?a4} "val7此时值是：$val7"
# val8=""
# echo ${val8:?a4} "val8此时值是：$val8"
# echo "end-----"

#模式替换
#${val%pattern}:检测字符串中是否满足同一的模式结尾，如果是则删除右边最短的匹配模式
val1=helloshellhelloshellhelloshell
echo ${val1%s*}
echo -e "\n"

#${val%%pattern}:检测字符串中是否满足同一的模式结尾，是就删除右边最长的匹配模式
val2=helloshellhelloshellhelloshell
echo ${val2%%s*}
echo -e "\n"

#${val#pattern}:检测字符串中是否满足同一的模式结尾，是就删除左边边最短的匹配模式
val3=helloshellhelloshellhelloshell
echo ${val3#*s}
echo -e "\n"

#${val##pattern}:检测字符串中是否满足同一的模式结尾，是就删除左边边最长的匹配模式
val4=helloshellhelloshellhelloshell
echo ${val4##*s}
echo -e "\n"

